<?php

include_once 'DB.php';

class Super_Model extends Application_Model_DB {

    public function select($table_name, $fields ='*', $where = '', $order_by = '', $limit = '') {
        if (!empty($table_name)) {
            $sql = 'SELECT ' . $fields . ' FROM `' . addslashes($table_name) . '`';
            if (!empty($where))
                $sql .=' WHERE ' . addslashes($where);
            if (!empty($order_by))
                $sql .=' ORDER BY ' . addslashes($order_by);
            if (!empty($limit))
                $sql .=' LIMIT ' . addslashes($limit);
            return $this->getAll($sql);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function selectAss($table_name, $fields ='*', $where = '', $order_by = '', $limit = '') {
        if (!empty($table_name)) {
            $sql = 'SELECT ' . $fields . ' FROM `' . addslashes($table_name) . '`';
            if (!empty($where))
                $sql .=' WHERE ' . $where;
            if (!empty($order_by))
                $sql .=' ORDER BY ' . addslashes($order_by);
            if (!empty($limit))
                $sql .=' LIMIT ' . addslashes($limit);
            //_debug($sql);
            return $this->getAssoc($sql);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function select_pn($table_name, $fields ='*', $where = '', $order_by = '', $offset = '', $limit = '', $left_join = '', $group_by = '') {
        if (!empty($table_name)) {
            if (!empty($where))
                $where = ' WHERE ' . $where;
            if (!empty($order_by))
                $order_by = ' ORDER BY ' . $order_by;
            if (!empty($group_by))
                $order_by = ' GROUP BY ' . $group_by;

            $limit = ' LIMIT ' . intval($offset) . (!empty($limit) ? (', ' . intval($limit)) : '');

            $sql = 'SELECT SQL_CALC_FOUND_ROWS ' . $fields . '
					FROM ' . $table_name . '
						' . $left_join . '
                                                ' . $where . '
                                                ' . $group_by . '
                                                ' . $order_by . '
                                                ' . $limit;
            //_debug($sql);
            $result = $this->getAll($sql);
            if (!empty($result)) {
                $total = $this->getOne("SELECT FOUND_ROWS()");
                return array('result' => $this->getAll($sql), 'total' => $total);
            }
            return FALSE;
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function getOneObj($table_name, $id_row) {
        if (!empty($table_name) && !empty($id_row)) {
            $sql = 'SELECT * FROM `' . addslashes($table_name) . '` WHERE `id` = ' . (int) $id_row;
            return $this->getRow($sql);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function update($table_name, $data, $debug=0) {
        if (isset($data['page'])) {
            unset($data['page']);
        }
        if (isset($data['onPage'])) {
            unset($data['onPage']);
        }
        if (isset($data['type_filter'])) {
            unset($data['type_filter']);
        }
        if (!empty($table_name) && !empty($data)) {
            $id = (int) $data['id'];
            if (!empty($id)) {
                unset($data['id'], $data['module'], $data['controller'], $data['action'], $data['event'], $data['order_field'], $data['order_dir']);
                if (!empty($data) && count($data)) {
                    $sql = 'UPDATE `' . $table_name . '` SET ';
                    $temp = array();
                    foreach ($data as $field => $value) {
                        $temp[] = '`' . $field . '` = \'' . addslashes(trim($value)) . '\' ';
                    }
                    $sql .= implode(',', $temp);
                    $sql .= 'WHERE `id` = ' . $id;
                  //  if ($debug)
                    //    _debug($sql);
                    return $this->exec($sql);
                }
            }
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function insert($table_name, $data, $debug=0) {
        if (isset($data['page'])) {
            unset($data['page']);
        }
        if (isset($data['onPage'])) {
            unset($data['onPage']);
        }
        // не сделано добавление меток
        if (!empty($table_name) && !empty($data)) {
            unset($data['module'], $data['controller'], $data['action'], $data['event']);
            if (!empty($data) && count($data)) {
                $sql = 'INSERT INTO `' . $table_name . '` SET ';
                $temp = array();
                foreach ($data as $field => $value) {
                    $temp[] = '`' . $field . '` = \'' . addslashes(trim($value)) . '\' ';
                }
                $sql .= implode(',', $temp);
                if ($debug)
                    _debug($sql);
                return $this->exec($sql);
            }
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function insertAttach($obj_type, $obj_id, $target_type, $target_id, $debug=0) {
        if (isset($obj_type) && isset($obj_id) AND isset($target_type) AND isset($target_id)) {
            $data = compact('obj_type', 'obj_id', 'target_type', 'target_id');
            $this->insert('attach', $data, $debug);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function removeAttach($obj_type, $obj_id, $target_type) {
        if (isset($obj_type) && isset($obj_id) AND isset($target_type)) {
            $sql = 'DELETE FROM `attach` WHERE ';
            $sql .= "`obj_type` = '" . addslashes($obj_type) .
                    "' AND `obj_id`  = '" . addslashes($obj_id) .
                    "' AND  `target_type` = '" . addslashes($target_type) . "'";
            return $this->Exec($sql);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    public function getAttach($obj_type, $obj_id, $target_type) {
        if (isset($obj_type) && isset($obj_id) AND isset($target_type)) {
            $sql = 'SELECT `target_id` FROM `attach` WHERE ';
            $sql .= "`obj_type` = '" . addslashes($obj_type) .
                    "' AND `obj_id`  = '" . addslashes($obj_id) .
                    "' AND  `target_type` = '" . addslashes($target_type) . "'";
            return $this->getAll($sql);
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }

    function maxsite_str_word($text, $counttext = 30, $sep = ' ') {
        $words = explode($sep, $text);
        if (count($words) > $counttext)
            $text = implode($sep, array_slice($words, 0, $counttext));
        return $text;
    }

    /* protected */

    protected function error($message) {
        die($message);
    }

}